package com.shuyu.testgsy

import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.NestedScrollView
import com.google.android.exoplayer2.SeekParameters
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.utils.Debuger
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer
import kotlinx.android.synthetic.main.activity_detail_player.*
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager
import java.util.*

class DetailPlayer : AppCompatActivity() {

    private var isPlay = false
    private var isPause = false
    private var orientationUtils: OrientationUtils? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail_player)

        val url = getUrl()
        val imageView = ImageView(this)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        imageView.setImageResource(R.mipmap.xxx1)




        resolveNormalVideoUI()
        //外部辅助的旋转，帮助全屏
        orientationUtils = OrientationUtils(this, detail_player)
        //初始化不打开外部的旋转
        orientationUtils!!.isEnable = false

        val header: MutableMap<String, String> =
            HashMap()
        header["ee"] = "33"
        header["allowCrossProtocolRedirects"] = "true"
        val gsyVideoOption = GSYVideoOptionBuilder()
        gsyVideoOption.setThumbImageView(imageView)
            .setIsTouchWiget(true)
            .setRotateViewAuto(false) //仅仅横屏旋转，不变直
            .setLockLand(false)
            .setAutoFullWithSize(false)
            .setShowFullAnimation(false)
            .setNeedLockFull(true)
            .setUrl(url)
            .setMapHeadData(header)
            .setCacheWithPlay(false)
            .setVideoTitle("测试视频")
            .setVideoAllCallBack(object : GSYSampleCallBack() {
                override fun onPrepared(
                    url: String,
                    vararg objects: Any
                ) {
                    Debuger.printfError("***** onPrepared **** " + objects[0])
                    Debuger.printfError("***** onPrepared **** " + objects[1])
                    super.onPrepared(url, *objects)
                    //开始播放了才能旋转和全屏
                    orientationUtils!!.isEnable = true
                    isPlay = true
                    //设置 seek 的临近帧。
                    if (detail_player!!.gsyVideoManager.player is Exo2PlayerManager) {
                        (detail_player!!.gsyVideoManager.player as Exo2PlayerManager).setSeekParameter(
                            SeekParameters.NEXT_SYNC
                        )
                        Debuger.printfError("***** setSeekParameter **** ")
                    }
                }

                override fun onEnterFullscreen(
                    url: String,
                    vararg objects: Any
                ) {
                    super.onEnterFullscreen(url, *objects)
                    Debuger.printfError("***** onEnterFullscreen **** " + objects[0]) //title
                    Debuger.printfError("***** onEnterFullscreen **** " + objects[1]) //当前全屏player
                }

                override fun onAutoComplete(
                    url: String,
                    vararg objects: Any
                ) {
                    super.onAutoComplete(url, *objects)
                }

                override fun onClickStartError(
                    url: String,
                    vararg objects: Any
                ) {
                    super.onClickStartError(url, *objects)
                }

                override fun onQuitFullscreen(
                    url: String,
                    vararg objects: Any
                ) {
                    super.onQuitFullscreen(url, *objects)
                    Debuger.printfError("***** onQuitFullscreen **** " + objects[0]) //title
                    Debuger.printfError("***** onQuitFullscreen **** " + objects[1]) //当前非全屏player
                    if (orientationUtils != null) {
                        orientationUtils!!.backToProtVideo()
                    }
                }
            })
            .setLockClickListener { view, lock ->
                if (orientationUtils != null) { //配合下方的onConfigurationChanged
                    orientationUtils!!.isEnable = !lock
                }
            }
            .setGSYVideoProgressListener { progress, secProgress, currentPosition, duration ->
                Debuger.printfLog(
                    " progress $progress secProgress $secProgress currentPosition $currentPosition duration $duration"
                )
            }
            .build(detail_player)
        detail_player!!.fullscreenButton
            .setOnClickListener {
                //直接横屏
                orientationUtils!!.resolveByClick()
                //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
                detail_player!!.startWindowFullscreen(this@DetailPlayer, true, true)
            }
    }

    override fun onBackPressed() {
        if (orientationUtils != null) {
            orientationUtils!!.backToProtVideo()
        }
        if (GSYVideoManager.backFromWindowFull(this)) {
            return
        }
        super.onBackPressed()
    }

    override fun onPause() {
        curPlay!!.onVideoPause()
        super.onPause()
        isPause = true
    }

    override fun onResume() {
        curPlay!!.onVideoResume(false)
        super.onResume()
        isPause = false
    }

    override fun onDestroy() {
        super.onDestroy()
        if (isPlay) {
            curPlay!!.release()
        }
        //GSYPreViewManager.instance().releaseMediaPlayer();
        if (orientationUtils != null) orientationUtils!!.releaseListener()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        //如果旋转了就全屏
        if (isPlay && !isPause) {
            detail_player!!.onConfigurationChanged(this, newConfig, orientationUtils, true, true)
        }
    }

    private fun resolveNormalVideoUI() { //增加title
        detail_player!!.titleTextView.visibility = View.GONE
        detail_player!!.backButton.visibility = View.GONE
    }

    private val curPlay: GSYVideoPlayer?
        private get() = if (detail_player!!.fullWindowPlayer != null) {
            detail_player!!.fullWindowPlayer
        } else detail_player


    private fun getUrl(): String {
        return "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4"
    }
}